quietly {


forv bs=0/$bootstraps {
	noi di "Bootstrap Iteration `bs', Day=`d', oo=mid $S_TIME  $S_DATE"



forv o=1/10 {
	
	forv d=1/3 {
	
		

		
clear
use ${data}Simulation/sim_data_d`d'_bs`bs'.dta
keep if oo==`o'


keep intDay  school_id schoolpos_id schoolcert_id NumberPositions teacher_id  u_t_oo_mid Rank_CM_tier_oo_mid Rank_School Rank_CM_true_oo_mid u_sch   round sch_outside_option outside_t pos 



* Run School Proposing DA Algorithm

gen da_match = Rank_School==1
gen hist = 1
* Run extra iterations to make sure proposals are exhausted
forv j=1/30 {
bys teacher_id: egen best = min(Rank_CM_true) if da_match==1
 replace da_match = . if Rank_CM_true>best
bys schoolpos_id : egen hold = max(da_match)
replace hist = hist+1 if hold==0 | missing(hold)
	replace da_match = 1 if Rank_School==hist & hist<.
drop best hold
bys teacher_id: egen best = min(Rank_CM_true) if da_match==1
 replace da_match = . if Rank_CM_true>best 
drop best
}
drop hist

* Run School Proposing DA Algorithm with tiered CM rankings
gen da_tier_match = Rank_School==1
gen hist = 1
forv j=1/30 {
bys teacher_id: egen best = min(Rank_CM_tier) if da_tier_match==1
 replace da_tier_match = . if Rank_CM_tier>best
bys schoolpos_id : egen hold = max(da_tier_match)
replace hist = hist+1 if hold==0 | missing(hold)
	replace da_tier_match = 1 if Rank_School==hist & hist<.
drop best hold
bys teacher_id: egen best = min(Rank_CM_tier) if da_tier_match==1
 replace da_tier_match = . if Rank_CM_tier>best 
drop best
}
drop hist




* Run Teacher Proposing DA Algorithm

gen tp_match = Rank_CM_true==1 if Rank_School<.
gen hist = 1
forv j=1/30 {
bys schoolpos_id : egen best = min(Rank_School) if tp_match==1
 replace tp_match = . if Rank_School>best & Rank_School<.
bys teacher_id: egen hold = max(tp_match)
replace hist = hist+1 if hold==0 | missing(hold)
	replace tp_match = 1 if Rank_CM_true==hist & hist<. & Rank_School<.
drop best hold
bys schoolpos_id : egen best = min(Rank_School) if tp_match==1
 replace tp_match = . if Rank_School>best & Rank_School<.
drop best
}
drop hist



* Random Serial Dicatorship
gen random=runiform()	
gsort teacher_id random
by teacher_id: gen Rank_rsd = _n
	

gen rsd_match = Rank_School==1
gen hist = 1
forv j=1/30 {
bys teacher_id: egen best = min(Rank_rsd) if rsd_match==1
 replace rsd_match = . if Rank_rsd>best
bys schoolpos_id : egen hold = max(rsd_match)
replace hist = hist+1 if hold==0 | missing(hold)
	replace rsd_match = 1 if Rank_School==hist & hist<.
drop best hold
bys teacher_id: egen best = min(Rank_rsd) if rsd_match==1
 replace rsd_match = . if Rank_rsd>best 
drop best
}
drop hist


* Snake Draft

* Locals with number of unique 
if `d'==1 {	
	local nschool = 32
	
}
if `d'==2 {
	local nschool = 28
	}
if `d'==3 {
	local nschool = 23
}


gen rand2 = runiform()
bys school_id: egen best_draw=max(rand2)
bys school_id: egen school_seq=seq()
gsort school_seq -best_draw
gen _draft_order = _n if school_seq==1
bys school_id: egen draft_order = max(_draft_order)

gen acceptable =  0
	replace acceptable = 1 if  u_sch>sch_outside_option & u_sch<. 
	
* Snake draft is slowest so limit iterations to just above maximum number of acceptable teachers	
bys school_id: egen tot_acceptable = sum(acceptable)
su tot_acceptable	
* 2 iterations per loop
local max_iter = ceil(r(max)/2)+1
 
* Round 1
gen draft_match = 0

forv rounds =1/ `max_iter' {
	


* Go down the order list

forv i=1/`nschool' {
	bys teacher_id: egen hired = max(draft_match)
	bys schoolpos_id: egen filled = max(draft_match)
	gen u_unhired = u_sch if hired==0
	bys school_id: egen best_available = max(u_unhired)
	replace draft_match = 1 if u_sch==best_available & acceptable==1 & draft_order==`i' & filled==0 & hired==0
	drop hired filled best_available u_unhired
}

* Go up the order list
forv i=1/`nschool' {
	local j = 33-`i'
	bys teacher_id: egen hired = max(draft_match)
	bys schoolpos_id: egen filled = max(draft_match)
	gen u_unhired = u_sch if hired==0
	bys school_id: egen best_available = max(u_unhired)
	replace draft_match = 1 if u_sch==best_available & acceptable==1 & draft_order==`j' & filled==0 & hired==0
	drop hired filled best_available u_unhired
}

 
}
 
 
 


replace tp_match = 0 if missing(tp_match)
replace da_match = 0 if missing(da_match)
replace da_tier_match = 0 if missing(da_tier_match)
replace rsd_match = 0 if missing(rsd_match)
replace draft_match = 0 if missing(draft_match)

keep schoolcert_id schoolpos_id teacher_id tp_match da_match da_tier_match rsd_match  draft_match 



save ${data}Simulation/sim_multi_d`d'_o`o'_bs`bs'.dta, replace
}
}
}





exit
